<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;
use app\shop\model\IndexCompany;
class IndexShopCar extends BaseModel
{
	//定义表名
	protected $name = 'longbing_card_shop_trolley';
    /**
     * @param $dis
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户信息
     */
	public function carList($dis){
        $list = $this
            ->alias('a')
            ->leftJoin('longbing_card_goods b' ,'a.goods_id = b.id')
            ->leftJoin('longbing_card_shop_spe_price c' ,'a.spe_price_id = c.id')
            ->where($dis)
            ->field(['a.*' ,'b.name,b.cover,b.price,b.freight,b.unit,b.is_self','round(c.price*a.number,2)as price','c.price as price2','b.is_send','c.stock','b.status as goods_status','b.type as goods_type'])
            ->order('a.id desc')
            ->group('a.id')
            ->select()
            ->toArray();
        $data['list'] = $list;
        $data['price']        = 0;
        $data['total_freight']= 0;
        $data['total_price']  = 0;
        if(!empty($list)){
            foreach ($list as &$v){
                $data['price']         += $v['price'];
                $data['total_freight'] += $v['freight'];
                $data['total_price']   += $v['price']+$v['freight'];
            }
        }
        $data  = transImages($data,['cover'],',');
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * 加入购物车
     */

    public function carAdd($data){
	    $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
	    return $res;
    }


    /**
     * @param $dis
     * @param $data
     * @return IndexShopCar
     * 编辑购物车
     */
    public function carUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }


    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取一个
     */
    public function carFind($dis){
        $data = $this->where($dis)->find();
        return $data;
    }

    /**
     * @param $ids
     * @return IndexShopCar
     * 批量删除
     */
    public function carDel($ids){
        $res = $this->where('id','in',$ids)->update(['status'=>-1]);
        return $res;

    }

    /**
     * @param $dis
     * 购物车数量
     */

    public function carCount($dis){
        $data = $this->where($dis)->count();
        return $data;
    }






	


}